<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html class="win-frame">
<head>
<title>${name_china}管理-新建${name_china}</title>
 <%@include file="/common/head.jsp" %>

</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
            <span class="split"></span>
            <span>新建${name_china}</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

<form id="addForm" method="post">
  <div id="left" style="width:80%;float:left">
	 <div class="form-wrapper">
		<div class="h4 distance-double">
	        <span class="h4-title"><i class="icon icon-setting"></i>基本信息</span>
	        <span class="h4-border"></span>
	    </div>
      	
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>推广位ID</p>
	        <input type="text" id="tgwId" name="tgwId" class="summer-input summer-validatebox" /> 
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>推广位名称</p>
	        <input type="text" id="tgwName" name="tgwName" class="summer-input summer-validatebox" /> 
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title">推广位PID</p>
	        <input type="text" id="tgwPid" name="tgwPid" class="summer-input"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title">微信ID</p>
	        <input type="text" id="unionid" name="unionid" class="summer-input summer-validatebox" /> 
	     </div>
	     
	    <div class="form-cont">
	        <p class="form-cont-title">电话</p>
	        <input type="text" id="phone" name="phone" class="summer-input summer-validatebox" /> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title">店主名称</p>
	        <input type="text" id="userNickName" name="userNickName" class="summer-input summer-validatebox" disabled/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title">用户ID</p>
	        <input type="text" id="userId" name="userId" class="summer-input summer-validatebox" disabled/> 
	     </div>
	     
	 <div class="form-btn-wrapper" id="funcbar">
        <a class="btn btn-normal" id="backBtn">关闭</a>
    </div>
  </div>
  </div>
	
</form>
<div class="search-field">
	<span class="field-cont">
        <span class="field-cont-title">用户名称</span>
        <input type="text" class="summer-input" id="userName" name="userName"/>
    </span>
    <span class="field-cont">
        <span class="field-cont-title">用户手机号</span>
        <input type="text" class="summer-input" id="userPhone" name="userPhone"/>
    </span>
     <span class="field-cont">
        <span class="field-cont-title">用户ID</span>
        <input type="text" id="id" name="id" class="summer-input summer-validatebox"/> 
        
    </span><br/>
   
    <span class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>
<div id="t-wrap">
 <table class="summer-datagrid" id="dataList">
	  <!--   <col width="30%">
	    <col width="30%">
	    <col width="30%">
	    <col width="30%">	 -->	    	    
	    <thead>
		     <tr>
		    <th><div class="summer-datagrid-cell">用户名称</div></th>
		    <th><div class="summer-datagrid-cell">用户ID</div></th>
		    <th><div class="summer-datagrid-cell">手机号</div></th>
		    <th><div class="summer-datagrid-cell">操作</div></th>
			 </tr>
	    </thead>
    </table>
    </div>
<script>
    var  setData,
    	 permission = CommonUtil.getPermission('${name_english}'),
   		 msg;
    $(function () {	
    	setData = ${setData};
    	msg = ${msg};
    	if(msg != "null"){
    		alert(msg);
    	}else{
    		if(setData != null){
        		$("#userId").val(setData.id);
        		$("#phone").val(setData.userPhone);
        		$("#userNickName").val(setData.userNickName);
        	}
    	}
    	
    	//初始化funcbar
     	$('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['${name_english}:add']
        }));
   	     	
    	//保存按钮
    	$('#funcbar').on('click', 'a[data-permit="${name_english}:add"]', {isContinue: false},  submit);
        $('#search').on('click', reloadTable);
    	//返回按钮
    	$('#backBtn,#goBack').on('click',backMain);
    	$('#reset').on('click', function() {
			//重置文本框的值
			$('#userName').val('');
			$('#userPhone').val('');
			$('#id').val('');
			reloadTable();
		});
    	createTable();
    	//设置推广位
		$('#t-wrap').on('click','a[data-permit="${name_english}:setStore"]',
				function() {
					var $this = $(this), 
					id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
					window.location.href = CONST_URL.SET_STORE + "?id="+id;
				});
    });
    
 // 重新加载表格
	function reloadTable() {
		createTable();
		$('#dataList').datagrid('reload', {
			ajax : {
				param : {
					"userPhone": trimSpace($('#userPhone').val()),
					"id": trimSpace($('#id').val())
				}
			}
		});
	}
  //创建表格
	function createTable() {
	  var userName = $('#userName').val();
		$('#dataList')
				.datagrid(
						{
							ajax : {
								url : CONST_URL.GET_PHONE+"?"+"userName="+userName,
								param : {	
									"userPhone": trimSpace($('#userPhone').val()),
									"id": trimSpace($('#id').val())
								},
								success : function(data) { //成功 
									alert("Data Saved: " + msg);
								}
							},
							columns : [
									{
										field : 'id',
										hidden : true
									},
									{
										field : 'userNickName'
									},
									{
										field : 'id'
									},
									{
										field : 'userPhone'
									},
									{
										render : function(rowData) {
											return $.funcbar({
														data : permission, //请求的数据
														type : 'text', //类型
														display : 1, //显示数量
														permit : [
																'${name_english}:setStore'
																 ]
													//需要的权限按钮
													});
										}
									}

							]
						});
	}
    //表单提交
    function submit(event) {
         $("#addForm").form({
        	 buttons : ['#saveBtn','#saveBtnContinue'],
             ajax: {
                 url: CONST_URL.ADD
             },
             onSubmitSuccess: function (data) {
            	 if(data.msg != "null"){
            		 alert(data.msg);
            	 }else{
            		 $.stip({
       	                type:'ok',//显示类型 有info、 ok、 danger
       	                text: '保存成功！',
       	                onBeforeHide: function () {
       	            	  var isContinue = event.data.isContinue;
                        	  if (isContinue) {
                               resetForm();
                        	  } else {
                        		  backMain();
                        	  }
       	               }
                   	});
            	 }
            	 
              }
         }).form("submit");
    }
    
    //保存并继续清空form
    function resetForm(){
        //$('#xm,#zw,#cs,#bgdh,#sjhm').val('');
    }
    
    //返回主页面
    function backMain() {
    	window.location.href = CONST_URL.INIT_MAIN;
    }
    
   
    //请求url
    var CONST_URL = {
    	INIT_MAIN: basePath + "${common_url}init",
       	ADD: basePath + "${common_url}add",
       	GET_PHONE: basePath + "${common_url}userList",
       	SET_STORE: basePath + "${common_url}setStore"
    };
    
</script>
</body>
</html>